package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.xxgc.eow.result.Result;

import java.util.ArrayList;
import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.IAnswerService;
import com.xxgc.eow.entity.Answer;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 答题提交记录 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-06-04
 */
@Tag(name = "答题提交记录")
@RestController
@RequestMapping("/answer")
public class AnswerController {

    @Resource
    private IAnswerService answerService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "查询所有接口", description = "查询答题提交记录所有数据")
    @SaIgnore
    @GetMapping
    public Result<List<Answer>> findAll() {
        return Result.ok(answerService.list());
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "根据id查询数据接口", description = "通过组件id查询")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/{id}")
    public Result<Answer> findOne(@PathVariable Integer id) {
        return Result.ok(answerService.getById(id));
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "分页查询接口", description = "分页查询")
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/page")
    public Result<Page<Answer>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        return Result.ok(answerService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 新增和更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "新增和更新接口", description = "传入id为更新,不传为新增,时间和有效字段均不填")
    @SaIgnore
    @PostMapping
    public Result save(@RequestBody Answer answer) {
        Integer answerId = answerService.getAnswerIdByUserIdAndTopicId(answer.getUserId(), answer.getTopicId());
        if(answerId != null){
            answer.setAnswerId(answerId);
        }
        answerService.saveOrUpdate(answer);
        return Result.ok();
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "删除接口", description = "通过id删除")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        answerService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "批量删除接口", description = "批量删除接口")
    @SaIgnore
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        answerService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 获取用户答题数
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "用户答题数", description = "用户答题数接口")
    @SaIgnore
    @GetMapping("/all")
    public Result findAllByUserId(Integer userId) {
        Integer num = answerService.getAllByUserId(userId);
        if(num == null){
            num = 0;
        }
        return Result.ok(num);
    }

    /**
     * 获取用户通过数
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "用户通过数", description = "用户通过数接口")
    @SaIgnore
    @GetMapping("/all/pass")
    public Result findAllPassByUserId(Integer userId) {
        Integer num = answerService.getAllPassByUserId(userId);
        if(num == null){
            num = 0;
        }
        return Result.ok(num);
    }

    /**
     * 获取用户错题数
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "用户未通过数", description = "用户未通过接口")
    @SaIgnore
    @GetMapping("/all/unPass")
    public Result findAllUnPassByUserId(Integer userId) {
        Integer num = answerService.getAllUnPassByUserId(userId);
        if(num == null){
            num = 0;
        }
        return Result.ok(num);
    }

    /**
     * 通过难度获取用户答题通过总数
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "用户对应难度通过数", description = "用户通过难度数接口")
    @SaIgnore
    @GetMapping("/all/level")
    public Result findAllPassByUserIdAndLevel(Integer userId,Integer level){
        Integer num = answerService.getCountByUserIdAndLevel(userId, level);
        if(num == null){
            num = 0;
        }
        return Result.ok(num);
    }

    /**
     * 通过用户id和题库id获取该题库的所有答题记录
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "用户题库答题记录", description = "用户题库答题记录接口")
    @SaIgnore
    @GetMapping("/allAnswer")
    public Result findAllAnswerByUserIdAndLibraryId(Integer userId,Integer libraryId){
        List<Answer> answerList = answerService.getAllAnswer(userId, libraryId);
        if(answerList.size() == 0){
            return Result.error(201,"无相关数据","");
        }
        return Result.ok(answerList);
    }

    /**
     * 清空一个用户的某个题库的答题记录
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "清空一个用户的答题记录", description = "清空一个用户的答题记录")
    @SaIgnore
    @GetMapping("/clearAnswer")
    public Result clearUserLibraryAnswer(Integer userId,Integer libraryId){
        answerService.clearUserAnswer(userId, libraryId);
        return Result.ok();
    }
}